﻿using System;
using System.Xml;
using System.Text;
using System.Reflection;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using Microsoft.SharePoint.WebControls;

namespace Voodoo.SharePoint.ReadUnreadMarks
{
    public class ReadUnreadListItemEventHandler : SPItemEventReceiver
    {
        //In an ideal world we would do all of this in ItemUpdateing and ItemAdding, however
        //the same events fire for document libraries as for items, and the order of firing
        //and avaiability of property collections is not guaranteed until the ItemAdded events
        //so we will do our updates here.
 

        public override void ItemAdded(SPItemEventProperties properties)
        {
            //DebugPause("ItemAdded");
            InitializeField(properties);
            base.ItemAdded(properties);
        }
 
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            //DebugPause("ItemUpdated");
            InitializeField(properties);
            base.ItemUpdated(properties);
        }

        void InitializeField(SPItemEventProperties properties)
        {
            if (UtilityMethods.CurrentConfigurationSupportsListType(properties.List))
            {
                ReadUnreadField ruField = UtilityMethods.FindFirstFieldOnList(properties.List);
                if (null != ruField)
                {
                    UtilityMethods.InitializeOrUpdateItem(properties.CurrentUserId, properties.ListItem, ruField);
                }
            }
        }
   
    }
}
